﻿<?php
/**
 * Get all subfolders of a given folder (or get a list of users.)
 */
 
// Return json encoded data
header ('Content-type: application/json');

session_start();
require_once '../core/db.php';

// Return all users of the system width registered folders
if (isset($_POST['find_users'])) {
	$sql = 'SELECT user, siteUsers.userID FROM siteUsers, folder where user=siteUsers.userID GROUP BY user ORDER BY user';
	$sth = $db->prepare ($sql);
	$sth->execute();
	die (json_encode ($sth->fetchAll ()));
}
// Return folders for a given user
if (isset($_POST['for_user'])) {
	$sql = 'SELECT name, id, user FROM folder WHERE user=? and parent=? order by name';
	$sth = $db->prepare ($sql);
	$sth->execute (array ($_POST['for_user'], $_POST['id']));
	die (json_encode  ($sth->fetchAll ()));
}
if (!isset($_SESSION['user']))	// We have not requested a given user, nor is a user logged in
	die (json_encode (array ('error'=>'No user logged on')));

// Return folders for the logged in user
$sql = 'SELECT name, id, user FROM folder WHERE user=? and parent=? order by name';
$sth = $db->prepare ($sql);
$sth->execute (array ($_SESSION['user'], $_POST['id']));
die (json_encode  ($sth->fetchAll ()));
?>